package com.bulk.demo.insert.service.impl;

import com.bulk.demo.insert.mapper.DeptMapper;
import com.bulk.demo.insert.mapper.UserMapper;
import com.bulk.demo.insert.service.IUserService;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.TimeUnit;

/**
 * @author 散装java
 * @since 2022/6/14
 */
@Service("userService")
@AllArgsConstructor
@Slf4j
public class UserServiceImpl implements IUserService {
    private final UserMapper userMapper;
    private final DeptMapper deptMapper;


    @SneakyThrows
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByName10s(String name) {
        int num = userMapper.updateByName(name);
        TimeUnit.SECONDS.sleep(10);
        log.info("======updateByName10s() 执行完毕，呕吼~");
        return num;
    }

    @Override
    public int updateByName(String name) {
        int num = userMapper.updateByName(name);
        log.info("======updateByName() 执行完毕");
        return num;
    }

    @SneakyThrows
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateUserAndDept() {
        deptMapper.updateById(1);
        // 模拟业务执行时间很长。
        TimeUnit.SECONDS.sleep(5);
        userMapper.updateById(1);
        log.info(">>先user再dept执行完毕");
    }

    @SneakyThrows
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDeptAndUser() {
        deptMapper.updateById(1);
        userMapper.updateById(1);
        // 修改一下
        log.info(">>先dept再user执行完毕，啊哈哈");
    }
}
